<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="消息标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入消息标题" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="操作人员" prop="creator">
        <el-input v-model="queryParams.creator" placeholder="请输入操作人员" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="发布时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:notice:add']">发布消息</el-button>
        <el-button type="danger" plain @click="handleBatchDelete" :disabled="ids.length == 0" class="batchDel" v-hasPermi="['system:notice:add']">批量删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="100" />
      <el-table-column label="消息标题" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="发布时间" align="center" prop="createTime" width="180" />
      <el-table-column label="发布人员" align="center" prop="creatorName" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="查看" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['system:notice:edit']"></el-button>
          </el-tooltip>

          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:notice:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-row v-show="total > 0">
      <el-col :span="12"> <pagination :total="total" v-model:page="queryParams.pageSize" v-model:limit="queryParams.pageCount" @pagination="getList" /></el-col>
    </el-row>

    <!-- 添加 -->
    <el-dialog :title="title" v-model="open" width="560px" append-to-body>
      <el-form ref="systemMessageRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="消息标题" prop="title">
              <el-input v-model="form.title" maxlength="10" show-word-limit placeholder="请输入消息标题" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="消息内容" prop="content">
              <el-input :rows="6" maxlength="100" show-word-limit type="textarea" placeholder="请输入消息内容" v-model="form.content" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog title="查看消息" class="scrollbar" v-model="detailVisible" width="560px" append-to-body>
      <div class="dialog-content">
        <h2 class="text-center">{{ form.title }}</h2>
        <p class="text-center">{{ form.createTime }}</p>
        <el-divider />
        <div class="padding-content">
          {{ form.content }}
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="SystemMessage">
import { list, add, detail, del, bacthDel } from "@/api/business/system-message";

const { proxy } = getCurrentInstance();
const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const detailVisible = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageCount: 10,
    pageSize: 1,
    title: "",
    creator: "",
    createTime: "",
  },
  rules: {
    title: [{ required: true, message: "消息标题不能为空", trigger: "blur" }],
    content: [{ required: true, message: "消息内容不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询消息列表 */
function getList() {
  loading.value = true;
  list(queryParams.value).then((response) => {
    noticeList.value = response.data.list;
    total.value = response.data.total_page;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    title: "",
    content: "",
  };
  proxy.resetForm("systemMessageRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageSize = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "发布消息";
}
/**详情 */
function handleDetail(row) {
  const ids = row.id || ids.value;
  detail(ids).then((response) => {
    form.value = response.data;
    detailVisible.value = true;
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["systemMessageRef"].validate((valid) => {
    if (valid) {
      add(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id;
  proxy.$modal
    .confirm('是否确认删除消息编号为"' + ids + '"的数据项？')
    .then(function () {
      return del(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 批量删除 */
function handleBatchDelete() {
  const data = ids.value;
  proxy.$modal
    .confirm('是否确认删除消息编号为"' + data.join(",") + '"的数据项？')
    .then(function () {
      return bacthDel({ ids: data });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>

<style scoped lang="scss">
.batchDel {
  // margin-top: 24px;
}
</style>
